package com.azzcs.userservicealibaba.controller;

import com.azzcs.userservicealibaba.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: wzg
 * @Date: 2021/2/20 上午11:21
 */
@RefreshScope
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${spring.datasource.url}")
    private String url;

    @GetMapping("/url")
    public String url(){
        return url;
    }

    @GetMapping("/login/{email}/{password}")
    public Boolean login(@PathVariable("email") String email,
                        @PathVariable("password")String password,
                        HttpServletResponse response){
        Boolean flag = userService.login(email,password);
        if(flag) {
            String token = userService.createToken(email);
            response.setHeader("token", token);
        }
        return flag;
    }

    @GetMapping("/info/{token}")
    public String info(@PathVariable("token") String token){
        return userService.getUserInfo(token);
    }

}
